<?php

/**
 * sfGuardRegisterForm for registering new users
 *
 * @package    sfDoctrineGuardPlugin
 * @subpackage form
 * @author     Jonathan H. Wage <jonwage@gmail.com>
 * @version    SVN: $Id: BasesfGuardChangeUserPasswordForm.class.php 23536 2009-11-02 21:41:21Z Kris.Wallsmith $
 */
class sfGuardRegisterForm extends BasesfGuardRegisterForm
{
  /**
   * @see sfForm
   */
  public function configure()
  {
      $this->setWidgets(array(
        'username'          => new sfWidgetFormInput(array('label'    => "Nom d'utilisateur")),
        'password'          => new sfWidgetFormInputPassword(array('label'    => "Mot de passe")),
        'password_again'    => new sfWidgetFormInputPassword(array('label'    => "Confirmer le mot de passe")),
        'email_address'     => new sfWidgetFormInput(array('label'    => "Votre adresse email"))
        , 'captcha'          => new sfWidgetFormReCaptcha(array('label'=>'Captcha Anti bot',
                                                               'public_key'=> sfConfig::get('app_recaptcha_public_key')))

      ));
      if(!sfConfig::get('app_active_recapatcha'))
          unset($this['captcha']);
      else
        $this->setValidator("captcha",  new sfValidatorReCaptcha(array('private_key'    =>  sfConfig::get('app_recaptcha_private_key'))));

      $this->setValidators(array(
          'username'  => new sfValidatorString(array(
                                                'required'  =>  true),
                                               array(                                                 
                                                   'required' => "Un nom d'utilisateur est obligatoire !"
                                                   
           )),
          'password'=> new sfValidatorString(array(
                                                'required'  =>  true,
                                                'min_length'=> 4),
                                            array(
                                                'required'  => "Un mot de passe est obligatoire !",
                                                'min_length'=> "Le mot de passe doit contenir au moins %min_length% caractéres !"
           )),
          'password_again'  =>  new sfValidatorString(array('required' => true), array( 'required' => 'Vous devez confirmer le mot de passe !')),
          'email_address' => new sfValidatorEmail(array('required'=> true), array('invalid' => "Ceci n'est pas une adresse email."))
      ));
  
      $this->validatorSchema->setPostValidator(
              new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, "password_again")
              );
      $this->widgetSchema->setNameFormat("ktd_player_register[%s]");
  }
  
  public function save($conn = null){
      $user = parent::save($conn);
      $user->setPseudo($user->getUsername());
      if(sfGuardUser::$DEFAULT_GROUP != NULL)
        $user->addGroupByName(sfGuardUser::$DEFAULT_GROUP);
      $user->save();
      return $user;
  }
}